// COutputPage.cpp : implementation file
//

#include "stdafx.h"
#include "avcap.h"
#include "COutputPage.h"

#include "CLiveCapture.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// COutputPage dialog


COutputPage::COutputPage(CWnd* pParent /*=NULL*/)
	: CDialog(COutputPage::IDD, pParent)
{
	//{{AFX_DATA_INIT(COutputPage)
	mImageHeight = 0;
	mImageWidth = 0;
	mGrabFolder = _T("");
	//}}AFX_DATA_INIT
	mLiveCapture = NULL;
}


void COutputPage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COutputPage)
	DDX_Control(pDX, IDC_COMBO_ENCODING_METHOD, mComboEncodings);
	DDX_Text(pDX, IDC_EDIT_IMAGE_HEIGHT, mImageHeight);
	DDX_Text(pDX, IDC_EDIT_IMAGE_WIDTH, mImageWidth);
	DDX_Text(pDX, IDC_EDIT_GRAB_FOLDER, mGrabFolder);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COutputPage, CDialog)
	//{{AFX_MSG_MAP(COutputPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COutputPage message handlers
void COutputPage::SetLiveCapture(CLiveCapture * inData)
{
	mLiveCapture = inData;
}

BOOL COutputPage::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	ASSERT(mLiveCapture);

	FillEncodingTypes();
	mLiveCapture->GetImageSize(&mImageWidth, &mImageHeight);
	mGrabFolder = mLiveCapture->GetGrabFolder();

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void COutputPage::FillEncodingTypes(void)
{
	mComboEncodings.ResetContent();
	mComboEncodings.AddString("AVI");
	mComboEncodings.SetItemData(0, ET_AVI);
	mComboEncodings.AddString("DIVX");
	mComboEncodings.SetItemData(1, ET_DIVX);

	Encoding_Type type = (Encoding_Type)mLiveCapture->GetEncodingType();
	for (int i = 0; i < mComboEncodings.GetCount(); i++)
	{
		long data = mComboEncodings.GetItemData(i);
		if (mLiveCapture->GetEncodingType() == data)
		{
			mComboEncodings.SetCurSel(i);
			break;
		}
	}
}

void COutputPage::EnterEncodingType(void)
{
	int index = mComboEncodings.GetCurSel();
	if (index != CB_ERR)
	{
		long data = mComboEncodings.GetItemData(index);
		mLiveCapture->SetEncodingType((Encoding_Type)data);
	}
}

void COutputPage::EnterImageSize(void)
{
	UpdateData(TRUE);
	mLiveCapture->SetImageSize(mImageWidth, mImageHeight);
}

void COutputPage::EnterGrabFolder(void)
{
	UpdateData(TRUE);
	mLiveCapture->SetGrabFolder(mGrabFolder);
}

void COutputPage::OnOK() 
{
	EnterImageSize();  // Save image size first!
	EnterEncodingType();
	EnterGrabFolder();

	CDialog::OnOK();
}
